package org.crag.cafca.gui.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

/**
 * Destroy a sessionn(and all its attributes), and redirect the user to the login page. 
 * @author Celien
 *
 */
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * Logger log4j. See log4j doc for more information.
	 */
	private static Logger log = Logger.getLogger(new Throwable().getStackTrace()[0].getClassName());
    
    public LogoutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    /**
     * Destroy the current session (and its attributes) and redirect the user to the index.html page. 
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String userName = (String) session.getAttribute("username"); 
		session.invalidate(); 		
		log.info("User "+userName+ " is well disconected");
		response.sendRedirect("index.html");
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
